<?php
namespace Mobile\Controller;

use Mobile\Model\tmpmsgModel;
use Mobile\Controller\WechatController;
use Think\Controller;

class MsgController extends Controller
{

    public $tmpmsgModel;
    public $Wechatobj;
    
    // 初始化连接微信
    public function __construct()
    {
      //  parent::__construct();
        $this->tmpmsgModel = new tmpmsgModel();
        $this->Wechatobj = new WechatController();
    }
    
    // 模板消息测试
    // 定时发送模板消息
    public function index()
    {

        $list_where = array(
            "status" => -1,
            "send_plan_time" => array("lt",time()+20*60),
           // "msgtype" => $msgtype
        );
        
        $task_list = $this->tmpmsgModel->gettmpmsg($list_where); // 取出数据

        if (empty($task_list)) {
            return_json(0, "DATA_EMPTY");
        }
        
        // 发送消息
        $total = count($task_list); // 总的条数
        $success_nums = 0;
        

        foreach ($task_list as $k => $msg) {
            
            $openid = $msg['openid'];
            if(!$openid){
                //关闭空数据
                $this->tmpmsgModel->where(array('id'=>$msg['id']))->setField('status',4);//假删除
                continue;
            }
            
            $content = $msg['content'];
            // 发送模板消息
            //判断发送时间
            if($msg['send_plan_time']>time() || ($msg['status'] >0)){
                continue;
            }
            
            //再次查询数据库判断状态
            $where_msg = array(
                'id' => $msg['id']
            );
            $dres = $this->tmpmsgModel->getDetail($where_msg); // 取出数据
            
            if($dres){
                $send_status = $dres["status"];
                if($send_status != -1){
                    continue;//已经发送过 或正在发送
                }else{
                    //正在发送
                    $data = array(
                        'send_time' => time(),
                        'status' => 3,//正在发送
                    );
                    $res = $this->tmpmsgModel->edittmpmsg($where_msg, $data);
                }
            }else{
                continue;//已被删除
            }
            
            $res = $this->Wechatobj->sendTemplateMessage($content);
            
            if (isset($res['errcode']) && ($res['errcode'] == 0)) {
                // 发送成功
                 $data = array(           
                     'send_time' => time(),
                     'status' => 1,
                     'errmsg' => $res['errmsg'],
                 );
              //   $res = $this->tmpmsgModel->edittmpmsg($where_msg, $data);
                
                $res = $this->tmpmsgModel->where($where_msg)->delete();//直接删除
                if($res){
                    $success_nums ++;
                }
                
            } else {
                // 发送失败
                 $data = array(
                     'send_time' => time(),
                     'status' => 2,
                     'errmsg' => $res['errmsg'],
                 );
                 if($res['errcode'] == '40001'){
                     $data['status'] = -1;
                 }
                 $this->tmpmsgModel->edittmpmsg($where_msg, $data);
 //               $res = $this->tmpmsgModel->where($where_msg)->delete();//直接删除
            }
        }
        
        $return = array(
            "success" => $success_nums,
            "total" => $total
        );
        
        return_json(0, "SUCCESS", $return);
    }
}